#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>

#define STACK_SIZE	2048 // struct elements
#define EMPTY		 (-1)

typedef struct var{
	char name[MAX_NAME_LEN];
	char data[MAX_DATA_LEN];
}var;

const var empty_var = {0};
var var_stack[STACK_SIZE] = {0};
int top = EMPTY;

bool push(var new_var) {
	if(top >= STACK_SIZE - 1) return false;
	var_stack[++top] = new_var;
	return true;
}

var pop() {
	if(top == EMPTY) return empty_var;
	return var_stack[top--];
}

bool check_arg(char* arg){
	if(arg[0] >= '0' && arg[0] <= '9')return false;
	
	char* p = arg;
	
	for(char c = 0; *p != '=' && *p != '\0'; p++){
		c = *p;
		if(c == '_') continue;
		if(c >= '0' && c <= '9') continue;
		c |= 0x20;	// ASCII case insensitive mask
		if(c >= 'a' && c <= 'z') continue;
		break;
	}
	if(*p == '=') return true;
	
	return false;
}

int main(int argc, char* argv[]) {
	for(int i = 1; i < argc; i++){
		if(check_arg(argv[i])){
			char* c = argv[i];
			var aux = {0};
			int j = 0;
			for(; c[j] != '=' && (j < MAX_NAME_LEN - 1); j++);
			strncpy(aux.name, argv[i], j);
			for(; c[j] != '=' && c[j] != '\0';j++);
			if(c[j] != '=')break;
			j++;
			strncpy(aux.data, &c[j], MAX_DATA_LEN - 1);
			if(push(aux) == false)break;
		}
	}
	
	var t = {0};
	
	while(true){
		t = pop();
		if(t.name[0] == '\0')break;
		printf("Name: %s, Value: %s\n", t.name, t.data);
	}
	return 1;
}
